package cn.wolfcode.trip.app.web.filter;


import cn.wolfcode.trip.app.util.UploadUtil;
import org.apache.commons.io.FileUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

public class ImageFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String imageUrl = req.getRequestURI();
        File file = new File(UploadUtil.IMG_BASE_PATH, imageUrl);
        if (file.exists()) {
            byte[] bytes = FileUtils.readFileToByteArray(file);
            response.getOutputStream().write(bytes);
        }else {
            chain.doFilter(request, response);
        }

    }

    public void destroy() {

    }
}
